package za.ac.wits.was.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import za.ac.wits.was.courses.Course;
import za.ac.wits.was.courses.allocations.CourseAllocations;
import za.ac.wits.was.courses.allocations.CourseAllocationsService;
import za.ac.wits.was.research.ResearchAllocations;
import za.ac.wits.was.research.ResearchAllocationsService;
import za.ac.wits.was.user.service.UserService;
import za.ac.wits.was.user.service.UserTimeService;

/**
 *
 * @author Salman Noor : School of Electrical and Information Engineering
 * @Copyright: Wits University 2013 - All Rights Reserved
 * @email: salmannoor74415@gmail.com
 * @License: BSD License (http://www.linfo.org/bsdlicense.html)
 */
@Controller
@RequestMapping("/allocations/*")
public class AllocationsController {

    Logger L = Logger.getLogger(AllocationsController.class);
    @Autowired
    private CourseAllocationsService courseAllocationsService;
    @Autowired
    private ResearchAllocationsService researchAllocationsService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserTimeService userTimeService;

    @RequestMapping(value = "/Courses", method = RequestMethod.GET)
    public String courses(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "CoursesAllocations");
        return "requestcourse";
    }

    @RequestMapping(value = "/Research", method = RequestMethod.GET)
    public String research(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "ResearchAllocations");
        return "requestresearch";
    }

    @RequestMapping(value = "/Personal", method = RequestMethod.GET)
    public String personalAllocations(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) {
        httpServletRequest.setAttribute("activeLink", "PersonalAllocations");
        return "personalallocations";
    }

    @RequestMapping(value = "/Personal/research", method = RequestMethod.GET)
    public @ResponseBody
    String personalResearchAllocations(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) throws IOException {
        List<ResearchAllocations> ra = this.userService.getAllResearch(httpServletRequest.getUserPrincipal().getName());
        return new ObjectMapper().writeValueAsString(ra);
    }

    @RequestMapping(value = "/Personal/time", method = RequestMethod.GET)
    public @ResponseBody
    String personalTimeManagment(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) throws IOException {
        this.userTimeService.calculateAllStats(httpServletRequest.getUserPrincipal().getName());
        return new ObjectMapper().writeValueAsString(this.userTimeService);
    }

    @RequestMapping(value = "/Personal/courses", method = RequestMethod.GET)
    public @ResponseBody
    String personalCourseAllocations(ModelMap model,
            final HttpServletRequest httpServletRequest, final HttpSession session) throws IOException {
        List<CourseAllocations> ca = this.userService.getAllCourses(httpServletRequest.getUserPrincipal().getName());
        return new ObjectMapper().writeValueAsString(ca);
    }

    @RequestMapping(value = "/Courses/read", method = RequestMethod.POST)
    public @ResponseBody
    String readDisciplines(final HttpServletRequest httpServletRequest) throws IOException {

        List<Course> ds = this.courseAllocationsService.getCourses(httpServletRequest.getUserPrincipal().getName());
        L.debug("Discipline List is :" + ds.toString());
        return new ObjectMapper().writeValueAsString(ds);
    }

    @RequestMapping(value = "/Courses/create", method = RequestMethod.POST)
    public @ResponseBody
    String createNewCourseAllocation(final HttpServletRequest httpServletRequest, @RequestParam("course_allocation") Long course_allocation,
            @RequestParam("start_date") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar start_date) throws IOException {

        CourseAllocations newCourseAllocations = this.courseAllocationsService.createNewCourseAllocations(course_allocation, httpServletRequest.getUserPrincipal().getName(), start_date);
        System.out.println(course_allocation.toString() + " " + start_date.toString());
        return new ObjectMapper().writeValueAsString(newCourseAllocations);
    }

    @RequestMapping(value = "/Research/create", method = RequestMethod.POST)
    public @ResponseBody
    String createNewResearchAllocation(final HttpServletRequest httpServletRequest,
            @RequestParam("researchstartdate") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar researchstartdate,
            @RequestParam("researchenddate") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar researchenddate,
            @RequestParam("sabbaticalstartdate") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar sabbaticalstartdate,
            @RequestParam("sabbaticalenddate") @DateTimeFormat(pattern = "yyyy-MM-dd") Calendar sabbaticalenddate,
            @RequestParam("research_hours") Integer research_hours,
            @RequestParam("research_sabbatical") Boolean research_sabbatical,
            @RequestParam("research_topic") String research_topic,
            @RequestParam("research_desciption") String research_desciption) throws IOException {
        Date sabStartTime = null;
        if (sabbaticalstartdate != null) {
            sabStartTime = sabbaticalstartdate.getTime();
        }
        Date sabEndTime = null;
        if (sabbaticalenddate != null) {
            sabEndTime = sabbaticalenddate.getTime();
        }
        ResearchAllocations newResearchAllocation =
                this.researchAllocationsService.createNewResearchAllocation(httpServletRequest.getUserPrincipal().getName(), research_topic, research_desciption, research_hours, research_sabbatical, researchstartdate.getTime(), researchenddate.getTime(), sabStartTime, sabEndTime);
        System.out.println(
                " " + research_hours.toString()
                + " " + research_topic.toString()
                + " " + research_desciption.toString());
        return new ObjectMapper().writeValueAsString(newResearchAllocation);
    }
}
